package com.xqyq.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.xqyq.model.Match;
import com.xqyq.model.Report;
import com.xqyq.model.Sign;
import com.xqyq.model.User;
import com.xqyq.service.FileService;
import com.xqyq.service.MatchService;
import com.xqyq.service.ReportService;
import com.xqyq.service.SignService;
import com.xqyq.util.CommonUtil;

@Controller
public class MatchContorller {
	@Autowired
	public SignService signService;

	@Autowired
	private MatchService matchService;

	@Autowired
	private FileService fileService;
	
	@Autowired
	private ReportService reportService;

	@RequestMapping("/match.html")
	public String match(@RequestParam("match_id") int match_id,
			HttpServletRequest request) {
		Match match = matchService.getTheWholeMatch(match_id);
		request.setAttribute("match", match);
		return "front/match";
	}

	@RequestMapping("/match_list.html")
	public String matchList(HttpServletRequest request) {
		
		
		// 最新赛事     6条
		List<Match> matchList = matchService.getPublishMatch(6);
		request.setAttribute("matchList", matchList);
		
		
		// 往期赛事 6条
		List<Report> reportList = reportService.getReports(6);
		request.setAttribute("reportList", reportList);
		return "front/match_list";
	}

	@RequestMapping("/user/myMatch.html")
	public String myMatch(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		List<Sign> signList = signService.getUserSignList(user.getUse_id());
		request.setAttribute("signList", signList);
		return "front/user_match";
	}

	@RequestMapping("/user_star.html")
	public String user_star(HttpServletRequest request) {
		List<Match> matchList = matchService.getEndMatch();
		request.setAttribute("matchList", matchList);
		return "front/user_star";
	}

	@RequestMapping("/api_sign")
	@ResponseBody
	public String api_sign(HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "{\"state\":104,\"comment\":\"请先登录。\",\"goPage\":\"login.html\"}";
		} else {
			if (CommonUtil.isEmpty(user.getDetailaddress())
					|| CommonUtil.isEmpty(user.getHead_photo())
					|| CommonUtil.isEmpty(user.getStudentId())
					|| CommonUtil.isEmpty(user.getMail())
					|| CommonUtil.isEmpty(user.getPhone())
					|| CommonUtil.isEmpty(user.getUniversity())
					|| CommonUtil.isEmpty("" + user.getGraduate_year())) {
				return "{\"state\":104,\"comment\":\"请先完善个人信息。\",\"goPage\":\"user/myzoom.html\"}";
			} else {
				int match_id = Integer.parseInt(request
						.getParameter("match_id"));
				int user_id = user.getUse_id();
				signService.sign(user_id, match_id);
				return "{\"state\":200,\"comment\":\"报名成功,可在个人中心查看。\",\"goPage\":\"\"}";
			}

		}
	}

	@RequestMapping("/uploadUserSign")
	public String uploadUserSign(
			@RequestParam("file") CommonsMultipartFile file,
			HttpServletRequest request, int match_id) {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "{\"state\":104,\"comment\":\"请先登录。\",\"goPage\":\"login.html\"}";
		} else {

			String name = "sign";
			String url = fileService.handleUploadData(name, file);
			if (url != null) {
				int user_id = user.getUse_id();

				System.out.println(match_id + "////" + user_id);

				signService.uploadUserSign(user_id, match_id);
				return "redirect:user/myhome.html";
			} else {
				return "{\"state\":104,\"comment\":\"上传失败。\",\"goPage\":\"\"}";

			}
		}
	}
	
	@RequestMapping(value = "/tianswcai.html")
	public String tianswcai(HttpServletRequest request) {
		
		List<Match> matchList = matchService.getPublishMatch(6);
		List<Match> matchPhotoList = new ArrayList<Match>();

		System.out.println(matchList.size());

		for (int i = 0; i < 4 && i < matchList.size(); i++) {
			matchPhotoList.add(matchList.get(i));

		}
		request.setAttribute("matchPhotoList", matchPhotoList);
		request.setAttribute("matchList", matchList);
		
		
		List<Report> reportList = reportService.getReports(6);
		List<Report> reportPhotoList = new ArrayList<Report>();
		for (int i = 0; i < 4 && i < reportList.size(); i++) {
			reportPhotoList.add(reportList.get(i));
		}
		request.setAttribute("reportPhotoList", reportPhotoList);
		request.setAttribute("reportList", reportList);
		
		
		return "front/tianswcai";

	}
}
